/**
 * @file   Custom.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-19
 * @brief  DON data type object: Custom
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/DataType/Custom.h>
#include <DON/Core/DataType/UInt16.h>
#include <QtCore/QByteArray>

using namespace DON::Core::DataType;

class Custom::Private
{
public:
    Private()
        : value(QByteArray())
    { }

    QByteArray value;
};

Custom::Custom()
    : AbstractDataType(),
      d(new Private())
{
}

Custom::Custom(const Custom &other)
    : AbstractDataType(),
      d(new Private())
{
    d->value = other.d->value;
}

Custom::~Custom()
{
    delete d;
}

Custom & Custom::operator=(const Custom &other)
{
    d->value = other.d->value;

    return *this;
}

AbstractDataType * Custom::clone() const
{
    return new Custom(*this);
}

DataType Custom::getDataType() const
{
    return DataType_Custom;
}

QByteArray Custom::getValue() const
{
    return d->value;
}

void Custom::setValue(const QByteArray &value)
{
    d->value = value;
}

bool Custom::fromBinary(const QByteArray &data, const int startIndex, int *size)
{
    bool success = false;
    QByteArray value;
    int processedDataSize = 0;

    if ((data.size() > 0) &&
        (startIndex >= 0) &&
        ((startIndex + 2) <= data.size()))
    {
        const int length = static_cast<int>(UInt16::fromBinary(data, startIndex, &success));

        if (success)
        {
            const int valueDataIndex = startIndex + 2;

            if ((valueDataIndex + length) <= data.size())
            {
                value = data.mid(valueDataIndex, length);
                processedDataSize = 2 + length;
                success = true;
            }
            else
            {
                success = false;
            }
        }
    }

    if (success)
    {
        d->value = value;

        if (size != NULL)
        {
            *size = processedDataSize;
        }
    }

    return success;
}

QByteArray Custom::toBinary() const
{
    const int length = d->value.length();

    QByteArray data;

    if (length < (1 << 16))
    {
        data.append(UInt16::toBinary(static_cast<quint16>(length)));
        data.append(d->value);
    }

    return data;
}

Custom * Custom::fromAbstractDataType(AbstractDataType *instance)
{
    Custom *outputInstance = NULL;

    if (instance != NULL)
    {
        if (instance->getDataType() == DataType_Custom)
        {
            outputInstance = static_cast<Custom *>(instance);
        }
    }

    return outputInstance;
}
